From c08f8f302631a924139bfda53f4410d5b9011b05 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 24 Jun 2014 15:23:03 -0700 Subject: [PATCH] Add support for branch/tag/rev options --- src/cargo/util/toml.rs | 10 ++- tests/test_cargo_compile_git_deps.rs | 128 +++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index e5fd43ee8..86ce05f72 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -71,6 +71,9 @@ pub struct DetailedTomlDependency { version: Option, path: Option, git: Option, + branch: Option, + tag: Option, + rev: Option } #[deriving(Encodable,Decodable,PartialEq,Clone)] @@ -122,9 +125,14 @@ impl TomlManifest { (Some(string.clone()), SourceId::for_central()) }, DetailedDep(ref details) => { + let reference = details.branch.as_ref().map(|b| b.clone()) + .or_else(|| details.tag.as_ref().map(|t| t.clone())) + .or_else(|| details.rev.as_ref().map(|t| t.clone())) + .unwrap_or_else(|| "master".to_str()); + let new_source_id = details.git.as_ref().map(|git| { // TODO: Don't unwrap here - let kind = GitKind("master".to_str()); + let kind = GitKind(reference.clone()); let url = url::from_str(git.as_slice()).unwrap(); let source_id = SourceId::new(kind, url); // TODO: Don't do this for path diff --git a/tests/test_cargo_compile_git_deps.rs b/tests/test_cargo_compile_git_deps.rs index aad1c3ddf..9c50a90c2 100644 --- a/tests/test_cargo_compile_git_deps.rs +++ b/tests/test_cargo_compile_git_deps.rs @@ -101,6 +101,134 @@ test!(cargo_compile_simple_git_dep { execs().with_stdout("hello world\n")); }) +test!(cargo_compile_git_dep_branch { + let project = project("foo"); + let git_project = git_repo("dep1", |project| { + project + .file("Cargo.toml", r#" + [project] + + name = "dep1" + version = "0.5.0" + authors = ["carlhuda@example.com"] + + [[lib]] + + name = "dep1" + "#) + .file("src/dep1.rs", r#" + pub fn hello() -> &'static str { + "hello world" + } + "#) + }).assert(); + + git_project.process("git").args(["checkout", "-b", "branchy"]).exec_with_output().assert(); + git_project.process("git").args(["branch", "-d", "master"]).exec_with_output().assert(); + + let project = project + .file("Cargo.toml", format!(r#" + [project] + + name = "foo" + version = "0.5.0" + authors = ["wycats@example.com"] + + [dependencies.dep1] + + git = "file://{}" + branch = "branchy" + + [[bin]] + + name = "foo" + "#, git_project.root().display())) + .file("src/foo.rs", main_file(r#""{}", dep1::hello()"#, ["dep1"])); + + let root = project.root(); + let git_root = git_project.root(); + + assert_that(project.cargo_process("cargo-build"), + execs() + .with_stdout(format!("{} git repository `file:{}`\n\ + {} dep1 v0.5.0 (file:{})\n\ + {} foo v0.5.0 (file:{})\n", + UPDATING, git_root.display(), + COMPILING, git_root.display(), + COMPILING, root.display())) + .with_stderr("")); + + assert_that(&project.root().join("target/foo"), existing_file()); + + assert_that( + cargo::util::process("foo").extra_path(project.root().join("target")), + execs().with_stdout("hello world\n")); +}) + +test!(cargo_compile_git_dep_tag { + let project = project("foo"); + let git_project = git_repo("dep1", |project| { + project + .file("Cargo.toml", r#" + [project] + + name = "dep1" + version = "0.5.0" + authors = ["carlhuda@example.com"] + + [[lib]] + + name = "dep1" + "#) + .file("src/dep1.rs", r#" + pub fn hello() -> &'static str { + "hello world" + } + "#) + }).assert(); + + git_project.process("git").args(["tag", "v0.1.0"]).exec_with_output().assert(); + git_project.process("git").args(["checkout", "-b", "tmp"]).exec_with_output().assert(); + git_project.process("git").args(["branch", "-d", "master"]).exec_with_output().assert(); + + let project = project + .file("Cargo.toml", format!(r#" + [project] + + name = "foo" + version = "0.5.0" + authors = ["wycats@example.com"] + + [dependencies.dep1] + + git = "file://{}" + tag = "v0.1.0" + + [[bin]] + + name = "foo" + "#, git_project.root().display())) + .file("src/foo.rs", main_file(r#""{}", dep1::hello()"#, ["dep1"])); + + let root = project.root(); + let git_root = git_project.root(); + + assert_that(project.cargo_process("cargo-build"), + execs() + .with_stdout(format!("{} git repository `file:{}`\n\ + {} dep1 v0.5.0 (file:{})\n\ + {} foo v0.5.0 (file:{})\n", + UPDATING, git_root.display(), + COMPILING, git_root.display(), + COMPILING, root.display())) + .with_stderr("")); + + assert_that(&project.root().join("target/foo"), existing_file()); + + assert_that( + cargo::util::process("foo").extra_path(project.root().join("target")), + execs().with_stdout("hello world\n")); +}) test!(cargo_compile_with_nested_paths { let git_project = git_repo("dep1", |project| { project -- 2.30.2